﻿using UnityEngine;
using System.Collections;

public class CubeBehavior : MonoBehaviour {

	private bool moveCube = false;
	public float time = 1.5f;
	public float waitTime = 1.5f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update ()
	{

	}

	public void activateCubeMovement()
	{
		moveCube = true;
		foreach(Transform child in transform)
		{
			Vector3 newPos = new Vector3(child.transform.position.x, child.transform.localScale.y/2, child.transform.position.z);
			StartCoroutine(	DoMove(child.gameObject, newPos, time));
		}

	}

	public IEnumerator DoMove(GameObject cube, Vector3 end, float time)
	{
		Vector3 initialPosition = cube.transform.position;
		while(moveCube)
		{
			iTween.MoveTo(cube, end, time);
			yield return new WaitForSeconds(waitTime);
			iTween.MoveTo(cube, initialPosition, time);
			yield return new WaitForSeconds(waitTime);
		}
	}
}
